import configparser
from openpyxl import Workbook, load_workbook
# 创建一个新的工作簿
wb = Workbook()
# 选择第一个工作表
ws = wb.active
# 写入表头
ws['A1'] = 'Section'

# 读取ini文件并写入到excel中
config = configparser.ConfigParser()
j=2
with open(r"E:\deskt\SS\配置文件写入Excel\1.txt", 'r') as file:
        for line in file:
            line = line.strip()# 去除行首行尾的空白字符（如换行符）
            if '=' in line:
                key, value = line.split('=')
                value=value+r'PVCTConfig\Default_Mono.cdf'
                ws.cell(row=j, column=1, value=key)
                
                config.read(value)
                # 获取所有的section
                sections = config.sections()
                # 遍历每个section并获取其键值对
                for section in sections:
                    options = config.options(section)
                    for option in options:
                        value = config.get(section, option)
                        # 检查是否已经存在该option
                        found = False
                        print(ws.max_column + 1)
                        for col in range(1, ws.max_column + 1):
                            cell = ws.cell(row=1, column=col)
                            if cell.value == option:
                                found = True
                                ws.cell(row=j, column=col, value=value)
                                break
                        # 如果没有找到，则在第一行末尾添加该option
                        if not found:
                            print("新加")
                            col = ws.max_column + 1
                            ws.cell(row=1, column=col, value=option)
                            ws.cell(row=j, column=col, value=value)
            wb.save(r'E:\deskt\SS\配置文件写入Excel\example.xlsx')
            j +=1
wb.save(r'E:\deskt\SS\配置文件写入Excel\example.xlsx')
